﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class categoryList : System.Web.UI.Page
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Category List";
            if (!IsPostBack)
            {

                loadData(1);
                DropDownList1.DataSource = dc.Languages;
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "id";
                DropDownList1.DataBind();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int languageId = Convert.ToInt32(DropDownList1.SelectedValue);
            loadData(languageId);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            HiddenField hf = lb.FindControl("HiddenField1") as HiddenField;
            int catID = Convert.ToInt32(hf.Value);
            int listPro = dc.Products.Where(z=>z.categoriesId==catID).ToList().Count;
            var cat = dc.Categories.SingleOrDefault(z => z.id == catID);
            if (listPro >0)
            {
               
                cat.status = false;
                dc.SubmitChanges();
            }
            else
            {
                var catLanguage = dc.CategoriesLanguages.Where(z=>z.categoriesId==catID);
                dc.CategoriesLanguages.DeleteAllOnSubmit(catLanguage);
                dc.Categories.DeleteOnSubmit(cat);
                dc.SubmitChanges();
                

            }
            Response.Redirect("categoryList.aspx");
        }

        private void loadData(int languageID)
        {


            var list = dc.CategoriesLanguages.Where(z => z.Category.status == true && z.languageId == languageID).ToList();


            ListView1.DataSource = list;
            ListView1.DataBind();
            if (list.Count<=0)
            {
                Button1.Visible = false;
            }



        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                foreach (ListViewItem item in ListView1.Items)
                {
                    HiddenField hd = item.FindControl("HiddenField1") as HiddenField;
                    TextBox textboxOrder = item.FindControl("TextBox1") as TextBox;

                  int cat =Convert.ToInt32(hd.Value);
                  var catLanguage = dc.Categories.SingleOrDefault(z=>z.id==cat);
                  if (!string.IsNullOrEmpty(textboxOrder.Text.Trim()))
                  {
                      catLanguage.orders = Convert.ToInt32(textboxOrder.Text);
                      dc.SubmitChanges();
                  }
                 
                }
                
            }
           
        }




    }
}